package com.example.wanbaqian.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */

public abstract class LoadMoreAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    private int head = 0x1111;
    private int item = 0x2222;
    private int foot = 0x3333;

    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;
    protected LayoutInflater mInflater;
    public LoadMoreAdapter(final Context context, final int layoutId, List<T> datas)
    {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mLayoutId = layoutId;
        mDatas = datas;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == head)
        {
            View itemView = mInflater.inflate(R.layout.view_empty, parent, false);
            AutoUtils.autoSize(itemView);
            return  ViewHolder.createViewHolder(mContext,itemView);
        }
        else if (viewType == item)
        {
            View itemView = mInflater.inflate(mLayoutId, parent, false);
            AutoUtils.autoSize(itemView);
            return  ViewHolder.createViewHolder(mContext,itemView);
        }
        else {
            View itemView = mInflater.inflate(R.layout.default_footer, parent, false);
            AutoUtils.autoSize(itemView);
            return  ViewHolder.createViewHolder(mContext,itemView);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (getItemViewType(position) == item) {
            convert(holder,mDatas.get(position),position);
        } else if (getItemViewType(position) == head) {
            convertEmpty(holder);
        } else if (getItemViewType(position) == foot) {
            TextView ooterTitle = holder.getView(com.liaoinstan.springview.R.id.default_footer_title);
            ProgressBar footerProgressbar =holder.getView(com.liaoinstan.springview.R.id.default_footer_progressbar);
            ooterTitle.setTextSize(12);
            if(loadmore_state==0)
            {
                ooterTitle.setText("加载中");
                footerProgressbar.setVisibility(View.VISIBLE);
                if(loadMoreListener!=null)loadMoreListener.onLoadMore();

            }else if(loadmore_state==1)
            {
                ooterTitle.setText("没有更多");
                footerProgressbar.setVisibility(View.GONE);
            }else if(loadmore_state==2)
            {
                ooterTitle.setText("加载错误");
                footerProgressbar.setVisibility(View.GONE);
            }else
                holder.itemView.setVisibility(View.INVISIBLE);

        }
    }

    protected void convertEmpty(ViewHolder holder){};

    @Override
    public int getItemViewType(int position) {
        if (position == 0&&mDatas.size()==0) {
            return head;
        }else if(position>=mDatas.size())
        {
            return foot;
        }
        else  {
            return item;
        }
    }
    @Override
    public int getItemCount() {
        return mDatas.size() > 0 ? mDatas.size()+1 : 1;
    }
    protected abstract void convert(ViewHolder holder, T t, int position);
    protected OnItemClickListener mOnItemClickListener;
    public interface OnItemClickListener {
        void onItemClick(View view, RecyclerView.ViewHolder holder,  int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public static final int STATE_INVALID = -1;
    public static final int STATE_LOAD_MORE = 0;
    public static final int STATE_NO_MORE = 1;
    public static final int STATE_ERROR = 2;
    public static final int STATE_GONE = 3;
    public int loadmore_state=STATE_LOAD_MORE;
    public void setCanLoadMore(int i)
    {
        loadmore_state=i;
    }
    public interface LoadMoreListener {
        void onLoadMore();
    }
    private LoadMoreListener loadMoreListener;
    public void setLoadMoreListener(LoadMoreListener listener) {
        this.loadMoreListener = listener;
    }
}
